import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.ImageIcon;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 * 
 * @author TamBV
 */
public class Poultry extends Sprite implements Commons {

	private Bomb bomb;

	private int type;
	public int direction = 1;
	public boolean spawnEgg = false;
	private float timespawn = 0;
	ImageIcon ii = null;
	int toX = 0;
	Timer myTimer;

	public Poultry(int type, int x, int y) {
		this.x = x;
		this.y = y;
		this.type = type;
		bomb = new Bomb(x, y);

		switch (type) {
		case 1:
			ii = new ImageIcon(getClass().getResource("Images/ga.png"));
			break;
		case 2:
			ii = new ImageIcon(getClass().getResource("Images/vit.png"));
			break;
		default:
			ii = new ImageIcon(getClass().getResource("Images/ngan.png"));
			break;
		}

		setImage(ii.getImage());

	}
	/*
	 *di chuyen gia cam theo theo phuong x.
	 *direction<0 di chuyen sang trai
	 *direction>0 di chuyen sang phai*/
	public void move(int direction) {
		this.x += direction;
	}

	/*
	 * This method handling direction of poultry between (x1, x2). After 5
	 * seconds message poultry can spawn.It spawn random egg or shit.
	 */
	public void handlePoultry(final int x1, final int x2) {
		toX = x1 + (int) (1 + (x2 - x1 - POULTRY_WIDTH) * Math.random());

		myTimer = new Timer();
		myTimer.schedule(new TimerTask() {

			@Override
			public void run() {
				if (getX() < toX) {
					direction = 1;
				} else {
					direction = -1;
				}
				if (getX() == toX) {
					toX = x1
							+ (int) (1 + (x2 - x1 - POULTRY_WIDTH)
									* Math.random());
				}
				timespawn += 17;
		
				if (timespawn > 5000) {
					int t = (int) ((11) * Math.random());
					if (t % 4 == 0)
						bomb.setBom(2);
					else
						bomb.setBom(1);
					spawnEgg = true;
					timespawn = 5;

				}
			}
		}, 0, 17);

	}

	public Bomb getBomb() {
		return bomb;
	}

	/*
	 *  Bomb la egg or shit*/
	public class Bomb extends Sprite {

		private boolean visible;
		public int typeBom = 1;

		public Bomb(int x, int y) {
			setVisible(false);
			this.x = x;
			this.y = y;

		}

		public void setBom(int type) {
			ImageIcon ii = null;
			if (type == 1) {
				typeBom = 1;
				ii = new ImageIcon(getClass().getResource("Images/egg.png"));
			} else {
				typeBom = 2;
				ii = new ImageIcon(getClass().getResource("Images/shit.png"));
			}

			setImage(ii.getImage());
		}
		// thiet lap trang thai hien/an cua Bom(egg/shit)
		public void setVisible(boolean visible) {
			this.visible = visible;
		}
		// xem trang thai hien/an cua Bom(egg/shit)
		public boolean isVisible() {
			return visible;
		}
	}
}
